/*
 *  Copyright © 2014 CIB Co.Ltd
 *  * All right reserved
 *  * Auther:jin xingwang
 *
 */

package com.wsd.scheduler;

import com.alibaba.fastjson.JSONObject;
import com.cif.service.CustomerService;
import com.cif.domain.Customer;
import com.wsd.domain.LoanLimit;
import com.wsd.service.LoanLimitService;
import org.apache.dubbo.config.annotation.Reference;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import static java.math.BigDecimal.ROUND_DOWN;

/**
 * 自动审核授信
 * @author 靳兴旺
 * @version 1.0.0 2020-7-21
 */
public class CreditCheckTimer extends QuartzJobBean {

    private final String SUCCESS = "1";

    private final String REJECT = "2";

    private final int LEVEL1 = 1000;
    private final int LEVEL2 = 3000;
    private final int LEVEL3 = 6000;
    private final int LEVEL4 = 9000;

    @Autowired
    private LoanLimitService loanLimitService;

    @Reference(version = "1.0.0", url = "${dubbo.service.cifurl}")
    private CustomerService customerService;

    static Logger logger = LoggerFactory.getLogger(CreditCheckTimer.class);

    /**
     * 任务执行代码
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("开始执行批量授信任务， 时间{}", LocalDateTime.now());
        List<LoanLimit> loanLimits = loanLimitService.applyingLoanLimit();
        Customer customer;
        for (LoanLimit loanLimit : loanLimits) {
            String userId = String.valueOf(loanLimit.getCustomerId());
            customer = JSONObject.parseObject(customerService.getCustomerById(userId)
                    .getData().toString(), Customer.class);
            creditCheck(customer.getIncome(), loanLimit);
        }
        loanLimitService.updateLoanLimit(loanLimits);
        logger.info("批量授信任务执行完成， 时间{}", LocalDateTime.now());
    }

    /**
     * 授信额度确认
     * @param income
     * @return
     */
    protected void creditCheck(BigDecimal income, LoanLimit loanLimit) {
        if (income.compareTo(BigDecimal.valueOf(LEVEL1)) < 0){
            setCredit(loanLimit, BigDecimal.ZERO, REJECT);
        } else if (income.compareTo(BigDecimal.valueOf(LEVEL2)) < 0) {
            setCredit(loanLimit, BigDecimal.valueOf(12000), SUCCESS);
        } else if (income.compareTo(BigDecimal.valueOf(LEVEL3)) < 0) {
            setCredit(loanLimit, BigDecimal.valueOf(18000), SUCCESS);
        } else if (income.compareTo(BigDecimal.valueOf(LEVEL4)) < 0) {
            setCredit(loanLimit, BigDecimal.valueOf(24000), SUCCESS);
        } else {
            BigDecimal creditLimit = income.multiply(BigDecimal.valueOf(6))
                    .divide(BigDecimal.valueOf(10000), 0, ROUND_DOWN)
                    .multiply(BigDecimal.valueOf(10000));
            setCredit(loanLimit, creditLimit, SUCCESS);
        }
    }

    /**
     * 设置用户授信信息
     * @param loanLimit
     * @param creditLimit
     * @param status
     */
    protected void setCredit(LoanLimit loanLimit, BigDecimal creditLimit, String status) {
        loanLimit.setLimitStatus(status);
        loanLimit.setMaxLimit(creditLimit);
        loanLimit.setAvailableLimit(creditLimit);
    }
}
